/***********************************************************************************************************************
* Copyright (C) . All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : main.c
* Version      :  
* Device(s)    : BAT32G157
* Tool-Chain   : MDK(armcc)
* Description  : This file is a template.
* Creation Date: 2019/4/30
***********************************************************************************************************************/

/***********************************************************************************************************************
Macro Definitions
***********************************************************************************************************************/

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include <stdio.h>
#include "BAT32G157.h"
#include "userdefine.h"
#include "sci.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
volatile uint32_t g_ticks;

void delayMS(uint32_t n)
{
		g_ticks = n;
		while(g_ticks);
}

int main()
{
	MD_STATUS status;
	uint32_t msCnt; 	// count value of 1ms
	uint8_t flag;

//-----------------------------------------------------------------------
// Systick setting 
//-----------------------------------------------------------------------   
	g_ticks = 1000; 	// 1000ms
	SystemCoreClockUpdate();
	msCnt = SystemCoreClock / 1000;
	SysTick_Config(msCnt); 

//-----------------------------------------------------------------------
// Init UART0 for retarget printf/scanf etc. 
//-----------------------------------------------------------------------
#if 1	
	SystemCoreClockUpdate();
	status = UART0_Init(SystemCoreClock, 19200);
	if(status == MD_ERROR)
	{
		while(1); // The baud rate cannot get at the current system clock frequency.
	}
	
	printf("Hello, UART\n");
#endif	
//-----------------------------------------------------------------------
// System Reset (SCB->AIRCR.SYSRESETREQ)
//----------------------------------------------------------------------- 
	flag = RST->RESF;
	printf("RESF = 0x%02X\n", flag);
	if(flag == 0x00)
	{
		NVIC_SystemReset();
	}
//-----------------------------------------------------------------------
// LED blinky on target board 
// PC02 drives LED on board
// PC01 drives LED on board
//----------------------------------------------------------------------- 
  PORT->PC    = 0x02U;  
	PORT->PMC  &= ~(3<<1); // PC02/LED, PC01/LED
	PORT->PMCC &= ~(3<<1);

	while(1)
	{
		delayMS(250);
		PORT->PC ^= (1<<1); 	// Toggle PC01
		PORT->PC ^= (1<<2); 	// Toggle PC02
	}
}

/***********************************************************************************************************************
* Function Name: SysTick Handler
* Description  : Decreament the g_ticks value
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void SysTick_Handler(void)
{
	g_ticks--;
}
